package com.jpyl.jpMedicalTreatment.view.activity.self;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;

import com.cjj.MaterialRefreshLayout;
import com.cjj.MaterialRefreshListener;
import com.google.gson.JsonObject;
import com.jpyl.jpMedicalTreatment.R;
import com.jpyl.jpMedicalTreatment.model.ErrorMessageModel;
import com.jpyl.jpMedicalTreatment.model.ProductsMessageModel;
import com.jpyl.jpMedicalTreatment.model.ResponseModel;
import com.jpyl.jpMedicalTreatment.network.Const;
import com.jpyl.jpMedicalTreatment.network.Network;
import com.jpyl.jpMedicalTreatment.network.api.ApiError;
import com.jpyl.jpMedicalTreatment.network.api.RequestApi;
import com.jpyl.jpMedicalTreatment.view.activity.BaseActivity;
import com.jpyl.jpMedicalTreatment.view.adapter.MyDiscountAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * 我的优惠
 * Created by 15697 on 2019/1/5.
 */

public class MyDiscountActivity extends BaseActivity {
    //    @BindView(R.id.tv_title)
//    TextView tvTitle;
    @BindView(R.id.rv_refresh_layout)
    RecyclerView rvRefreshLayout;
    @BindView(R.id.mr_refresh_layout)
    MaterialRefreshLayout mrRefreshLayout;
    @BindView(R.id.tv_refreshLayout_Empty)
    TextView tvRefreshLayoutEmpty;

    MyDiscountAdapter adapter;
    int flag;
    final RequestApi publicApi = Network.getRequestApi();

    int page = 1, listSize;
    private List<ProductsMessageModel.ProductsModel> rows = new ArrayList<>();

    @Override
    protected int getLayoutId() {
        return R.layout.activity_my_discount;
    }

    @Override
    public void afterCreate(Bundle savedInstanceState) {
        flag = getIntent().getIntExtra("flag", -1);
        switch (flag) {
            case Const.MYDISCOUNT:
                mTitleTv.setText(getString(R.string.mine_myDiscount));
                jpylGetSaleProducts();
                break;
            case Const.MYPRISE:
                mTitleTv.setText(getString(R.string.mine_myPrize));
                jpylGetBypointProducts();
                break;
        }
        adapter = new MyDiscountAdapter(rows, flag);
        rvRefreshLayout.setAdapter(adapter);
        adapter.setItemClickListener(new MyDiscountAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {

            }
        });

        rvRefreshLayout.setLayoutManager(new GridLayoutManager(this, 1));
        mrRefreshLayout.setLoadMore(true);
        mrRefreshLayout.setMaterialRefreshListener(new MaterialRefreshListener() {
            @Override
            public void onRefresh(MaterialRefreshLayout materialRefreshLayout) {
                //刷新
                page = 1;
                switch (flag) {
                    case Const.MYDISCOUNT:
                        jpylGetSaleProducts();
                        break;
                    case Const.MYPRISE:
                        jpylGetBypointProducts();
                        break;
                }

            }

            @Override
            public void onRefreshLoadMore(MaterialRefreshLayout materialRefreshLayout) {
                super.onRefreshLoadMore(materialRefreshLayout);
                //加载更多
                if (10 == listSize) {
                    ++page;
                    switch (flag) {
                        case Const.MYDISCOUNT:
                            jpylGetSaleProducts();
                            break;
                        case Const.MYPRISE:
                            jpylGetBypointProducts();
                            break;
                    }
                } else {
                    materialRefreshLayout.finishRefreshLoadMore();
                }
            }
        });

    }

    /**
     * 我的优惠列表
     */
    private void jpylGetSaleProducts() {
        final JsonObject body = new JsonObject();
        body.addProperty("rows", "10");
        body.addProperty("page", page + "");

        publicApi.jpylGetSaleProducts(body.toString()).map(new Func1<ResponseModel<ProductsMessageModel>, ProductsMessageModel>() {
            @Override
            public ProductsMessageModel call(ResponseModel<ProductsMessageModel> responseModel) {
                if (responseModel.getCode() != 0) {
                    ErrorMessageModel messageModel = new ErrorMessageModel(responseModel.getMsg());
                    throw new ApiError(messageModel);
                }
                return responseModel.getData();
            }
        })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<ProductsMessageModel>() {
                    @Override
                    public void onCompleted() {
                        mrRefreshLayout.finishRefreshing();
                        mrRefreshLayout.finishRefresh();
                        mrRefreshLayout.finishRefreshLoadMore();
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                        mrRefreshLayout.finishRefreshing();
                        mrRefreshLayout.finishRefresh();
                        mrRefreshLayout.finishRefreshLoadMore();
                        if (e instanceof ApiError) {
                            ApiError error = (ApiError) e;
                        } else {
                        }
                    }

                    @Override
                    public void onNext(ProductsMessageModel model) {
                        mrRefreshLayout.finishRefreshing();
                        mrRefreshLayout.finishRefresh();
                        mrRefreshLayout.finishRefreshLoadMore();
                        listSize = model.getTotal();
                        if (page == 1) {
                            rows.clear();
                        }
                        rows.addAll(model.getProducts());
                        adapter.notifyDataSetChanged();
                    }
                });
    }

    /**
     * 我的奖品
     */
    private void jpylGetBypointProducts() {

        final JsonObject body = new JsonObject();
        body.addProperty("rows", "10");
        body.addProperty("page", page + "");

        publicApi.jpylGetBypointProducts(body.toString()).map(new Func1<ResponseModel<ProductsMessageModel>, ProductsMessageModel>() {
            @Override
            public ProductsMessageModel call(ResponseModel<ProductsMessageModel> responseModel) {
                if (responseModel.getCode() != 0) {
                    ErrorMessageModel messageModel = new ErrorMessageModel(responseModel.getMsg());
                    throw new ApiError(messageModel);
                }
                return responseModel.getData();
            }
        })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<ProductsMessageModel>() {
                    @Override
                    public void onCompleted() {
                        mrRefreshLayout.finishRefreshing();
                        mrRefreshLayout.finishRefresh();
                        mrRefreshLayout.finishRefreshLoadMore();
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                        if (e instanceof ApiError) {
                            ApiError error = (ApiError) e;
                        } else {
                        }
                        mrRefreshLayout.finishRefreshing();
                        mrRefreshLayout.finishRefresh();
                        mrRefreshLayout.finishRefreshLoadMore();
                    }

                    @Override
                    public void onNext(ProductsMessageModel model) {
                        mrRefreshLayout.finishRefreshing();
                        mrRefreshLayout.finishRefresh();
                        mrRefreshLayout.finishRefreshLoadMore();
                        listSize = model.getTotal();
                        if (page == 1) {
                            rows.clear();
                        }
                        rows.addAll(model.getProducts());
                        adapter.notifyDataSetChanged();
                    }
                });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

//    @OnClick(R.id.tv_title_back)
//    public void onViewClicked() {
//        finish();
//    }
}
